using System.IO;
using System.Collections.Generic;
using com.tc.assistance.parser;
using com.tc.assistance.protocol.entity;
using com.tc.util;
using com.tc.zigbee.entity.device;
using System.Xml;
using System.IO;
using com.tc.gateway.device;
namespace com.tc.zigbee.entity.device.config
{
public class DeviceConfigManager {
	static DeviceConfigManager instance;
	static DeviceConfigManager(){
		instance = new DeviceConfigManager();
	}
	static public DeviceConfigManager getInstance(){
		return instance;
	}
	
	Dictionary<string, Dictionary<string, DeviceConfig>> configs = new Dictionary<string, Dictionary<string, DeviceConfig>>();
	public void load(Stream ind){
         XmlReader reader = XmlReader.Create(ind);
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
		
		XmlNodeList vs = doc.GetElementsByTagName("node");
		for(int i = 0; i < vs.Count; i++){
			XmlElement v = (XmlElement)vs.Item(i);
			string ieee = v.GetAttribute("ieee");
			Dictionary<string, DeviceConfig> node = configs[ieee];
			if(node == null){
                node = new Dictionary<string, DeviceConfig>();
				configs.Add(ieee, node);
			}
			XmlNodeList ds = doc.GetElementsByTagName("device");
			for(int j = 0; j < ds.Count; j++){
				XmlElement d = (XmlElement)ds.Item(i);
				string ep = d.GetAttribute("endpoint");
				DeviceConfig dc = new DeviceConfig();
				dc.setExport( bool.Parse(d.GetAttribute("export")));
				dc.setName( d.GetAttribute("name"));
				node.Add(ep, dc);
			}
		}
	}
	public void save(Stream outd){
        /*
		XmlSerializer serializer = ParserFactory.getSerializer();
			serializer.setOutput(out, "UTF-8");
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "deviceConfigs");
			for(string n : configs.keySet()){
				Map<string, DeviceConfig> node = configs.get(n);
				serializer.startTag("", "node");
				serializer.attribute("", "ieee", n);
				
				for(string d : node.keySet()){
					DeviceConfig dc = node.get(d);
					serializer.startTag("", "device");
					serializer.attribute("", "endpoint", d);
					serializer.attribute("", "name", dc.name);
					serializer.attribute("", "export", dc.export + "");
					serializer.endTag("", "device");
				}
				
				serializer.endTag("", "node");
			}
			serializer.endTag("", "deviceConfigs");
			serializer.endDocument();
         * */
	}
	public void match(ZigbeeDevice d){
		string ieee = DataUtil.hex(d.getEndpoint().getParent().getIeeeAddress());


        Dictionary<string, DeviceConfig> node = null;
        if(configs.ContainsKey(ieee))
            node = configs[ieee];
		if(node == null){
			node = new Dictionary<string, DeviceConfig>();
			configs.Add(ieee, node);
		}
        DeviceConfig dc = null;
        if(node.ContainsKey("" + d.getEndpoint().getEndpointId()))
            dc = node["" + d.getEndpoint().getEndpointId()];
		if(dc == null){
			dc = new DeviceConfig();
			node.Add(DataUtil.hex(d.getEndpoint().getEndpointId()), dc);
		}
		d.setConfig(dc);
		
	}
}
}